/*
 * hmi.c
 *
 *  Created on: Sep 23, 2021
 *      Author: soliber
 */
#include "hmi.h"
#include "cat1.h"
#include <stdlib.h>
#include <string.h>
#include "deca_device_api.h"

//用于停止其他任务或外设
extern void spl_stop(void);
extern osThreadId presTaskHandle;
extern osThreadId rangeTaskHandle;
extern osThreadId voltageTaskHandle;

enum{SAFE,ALARM,RESCUE} mode = SAFE;

void blink_cb(void const * argument);
void alarm_cb(void const * argument);

//报警定时器
osTimerDef(alarm,alarm_cb);
osTimerId alarmTimer;
void alarm_cb(void const * argument)
{
	HAL_GPIO_TogglePin(RED_GPIO_Port, RED_Pin);
}

/**
 * 本Task负责人机界面的控制，包括指示灯和报警按键的相关操作
 * 其他任务使用ITC与本任务通信，来控制HMI相关器件
 */
void StartHMITask(void const * argument)
{
	uint32_t button_time=0;
	/**
	 * 黄色指示灯用于指示MQTT状态，只有MQTT服务正常工作时黄色指示灯才熄灭
	 */
	LED_ENABLE();
	LED_YELLOW_ON();
	LED_RED_OFF();
	LED_GREEN_OFF();
	RESCUE_OFF();

	//定时器初始化
	alarmTimer = osTimerCreate(osTimer(alarm), osTimerPeriodic, &mode);

	while(1)
	{
		osEvent evt = osSignalWait(SIGNAL_MQTT_OFFLINE | SIGNAL_MQTT_ONLINE |
				SIGNAL_ALARM | SIGNAL_RESCUE | SIGNAL_SAFE |
				SIGNAL_BUTTON_PRESS | SIGNAL_BUTTON_RELEASE, osWaitForever);
		if(evt.status == osEventSignal)
		{
			if(evt.value.signals&SIGNAL_MQTT_OFFLINE)
			{
				LED_ENABLE();
				LED_YELLOW_ON();
			}
			if(evt.value.signals&SIGNAL_MQTT_ONLINE)
			{
				LED_YELLOW_OFF();
			}
			if(mode == SAFE && evt.value.signals&SIGNAL_ALARM)
			{
				mode = ALARM;
				LED_ENABLE();
				osTimerStart(alarmTimer, 500);
				if(publish_enable)
				{
					char *p = malloc(sizeof("{\\\\22mode\\\\22:\\\\22alarm\\\\22}"));
					if(p==NULL) return;
					strcpy(p,"{\\\\22mode\\\\22:\\\\22alarm\\\\22}");
					osMessagePut(publish_q_id, (uint32_t)p, 0);
				}
			}
			if(mode == ALARM && evt.value.signals&SIGNAL_RESCUE)
			{
				mode = RESCUE;
				if(publish_enable)
				{
					char *p = malloc(sizeof("{\\\\22mode\\\\22:\\\\22rescue\\\\22}"));
					if(p==NULL) return;
					strcpy(p,"{\\\\22mode\\\\22:\\\\22rescue\\\\22}");
					osMessagePut(publish_q_id, (uint32_t)p, 0);
				}
				osTimerStop(alarmTimer);
				LED_ENABLE();
				LED_RED_ON();
				RESCUE_ON();
				osDelay(500);
				osThreadTerminate(presTaskHandle);
				osThreadTerminate(rangeTaskHandle);
				osThreadTerminate(voltageTaskHandle);
				dwt_rxreset();
				dwt_entersleep();
				spl_stop();
#ifndef DEBUG
				HAL_GPIO_WritePin(CAT1_EN_GPIO_Port, CAT1_EN_Pin, GPIO_PIN_SET);
				HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFE);
#endif
				while(1);
			}
			if(evt.value.signals&SIGNAL_SAFE)
			{
				mode = SAFE;
				osTimerStop(alarmTimer);
				LED_DISABLE();
				LED_YELLOW_OFF();
				LED_RED_OFF();
				LED_GREEN_OFF();
				RESCUE_OFF();
				if(publish_enable)
				{
					char *p = malloc(sizeof("{\\\\22mode\\\\22:\\\\22safe\\\\22}"));
					if(p==NULL) return;
					strcpy(p,"{\\\\22mode\\\\22:\\\\22safe\\\\22}");
					osMessagePut(publish_q_id, (uint32_t)p, 0);
				}
			}
			if(mode == SAFE && evt.value.signals&SIGNAL_BUTTON_PRESS)
			{
				button_time = HAL_GetTick();
			}
			if(mode == SAFE && evt.value.signals&SIGNAL_BUTTON_RELEASE)
			{
				if(HAL_GetTick()-button_time >= BUTTON_HOLD_TIME)
				{
					DEBUG_PRINT("Button Trigger Report!\r\n");
					if(publish_enable)
					{
						char *p = malloc(sizeof("{\\\\22event\\\\22:\\\\22button\\\\22}"));
						if(p==NULL) return;
						strcpy(p,"{\\\\22event\\\\22:\\\\22button\\\\22}");
						osMessagePut(publish_q_id, (uint32_t)p, 0);
					}
				}
			}
		}
	}

}
